package cn.wolfcode.web.service.impl;

import cn.wolfcode.web.domain.Employee;
import cn.wolfcode.web.mapper.EmployeeMapper;
import cn.wolfcode.web.page.PageResult;
import cn.wolfcode.web.query.EmployeeQueryObject;
import cn.wolfcode.web.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }


    @Override
    public void save(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> listAll() {

        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKey(employee);
    }



    @Override
    public PageResult query(EmployeeQueryObject qo) {

        int rows = employeeMapper.queryForCount(qo);
        if (rows==0) {
            //        (int currentPage,int  pageSize,int  rows,List<?>  result)
            return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),0, Collections.emptyList());
        }

        List<Employee> employeeList = employeeMapper.queryForList(qo);
        return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),rows,employeeList);
    }

    @Override
    public void insertRelation(Long id, Long roleId) {
        employeeMapper.insertRelation(id,roleId);
    }

}
